<template>
  <el-form ref="activeRole" :model="activeRole" :rules="rules" label-width="80px" style="width: 90%;margin:5% ">
    <el-form-item label="角色名称">
      <el-input v-model="activeRole.roleName"></el-input>
    </el-form-item>
    <el-form-item label="角色代码">
      <el-input v-model="activeRole.roleCode"></el-input>
    </el-form-item>
    <el-form-item label="描述">
      <el-input type="textarea" v-model="activeRole.description"></el-input>
    </el-form-item>
    <el-form-item label="状态">
      <el-radio-group v-model="activeRole.isdelete">
        <el-radio label="1">可用</el-radio>
        <el-radio label="0">删除</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<style>
</style>
<script>
  import roleApi from '../../../api/sys/roleApi'

  export default{
    data () {
      return {
        activeRole: {},
        rules: {
          roleName: [
            { required: true, message: '请输入角色名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 5 到 10 个字符', trigger: 'blur' }
          ],
          roleCode: [
            { required: true, message: '请输入角色代码', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      if (this.$route.params.id !== 'create') {
        roleApi.getRoleById(this.$route.params.id).then((resp) => {
          this.activeRole = resp.body.data
        })
      } else {
        this.activeRole.isdelete = '1'
      }
    },
    methods: {
      onSubmit () {
        roleApi.saveRole(this.activeRole).then((data) => {
          this.$message({
            showClose: true,
            message: '保存成功'
          })
        })
      },
      cancel () {
        this.$router.push('/role')
      }
    }
  }
</script>
